package com.itheima.bank.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.bank.dao.BankDao;

import com.itheima.bank.domain.Account;
import com.itheima.bank.domain.Trade;
import com.itheima.bank.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = BankService.class)
//@Service
@Transactional
public class BankServiceImpl implements BankService {

    @Autowired
    BankDao bankDao;

    /**
     * 验证用户登陆
     */
    public Account userLogin(Account account) {
        if (null == account || account.getAccountid() == null || account.getPassword() == null || "".equals(account.getAccountid()) ||
                "".equals(account.getPassword())) {
            return null;
        }
        return bankDao.userLogin(account);
    }

    /**
     * 查询用户余额
     */
    public Double selectRemaining(String accountid) {
        if (null == accountid || "".equals(accountid))
            return null;
        return bankDao.selectRemaining(accountid);
    }

    /**
     * 查询交易记录
     */
    public List<Trade> selectTrade(String accountid, String startTime, String endTime) {
        return bankDao.selectTrade(accountid, startTime, endTime);
    }


    /**
     * 用户转账
     */
    public String accountTransaction(String accountid, String toaccountid, Double money) {
        // 手续费
        Double serviceCharge = 0d;
        // 余额
        Double remainning = 0d;

        if (accountid.equals(toaccountid)) {
            return "自己不要给自己转账噢";
        }
        // 查询转账对象是否存在
        Account _account = bankDao.selectUser(toaccountid);
        if (_account == null) {
            return "转账对象不存在,无法完成转账";
        }
        // 查询余额
        remainning = bankDao.selectRemaining(accountid);
        // 计算手续费
        serviceCharge = money * 0.005;
        if (serviceCharge < 2) {
            serviceCharge = 2.00;
        } else if (serviceCharge > 20) {
            serviceCharge = 20.00;
        }
        if (remainning < (money + serviceCharge)) {
            return "余额不足,无法完成转账";
        }

        // 给转账人扣钱
        bankDao.transMoney(accountid, -money - serviceCharge);
        //System.out.println(1 / 0);
        // 给收款人加钱
        bankDao.transMoney(toaccountid, money);
        // 添加转账记录
        // 0 存款 1 取款 2 转账
        // 转账人记录
        Trade trade =
                Trade.builder().accountId(accountid).tradeType("2").tradeMoney(money).tradeTime(new Date()).tradeDigest(toaccountid +
                        "手续费是" + serviceCharge).build();
        bankDao.addTrade(trade);
        // 收款人记录
        Trade trade2 =
                Trade.builder().accountId(toaccountid).tradeType("2").tradeMoney(money).tradeTime(new Date()).tradeDigest("从" + accountid + "账户转入" + money).build();
        bankDao.addTrade(trade2);

        return "转账成功了-- 转账金额   " + money + " 元 手续费  " + serviceCharge + " 元   余额还剩下  " + (remainning - serviceCharge - money) + " 元";
    }

    /**
     * 给用户存钱
     */
    public String savemoney(String accountid, Double savemoney) {

        // 存钱
        bankDao.transMoney(accountid, savemoney);
        // 0 存款 1 取款 2 转账
        // 添加记录
        Trade trade =
                Trade.builder().accountId(accountid).
                        tradeType("0").
                        tradeMoney(savemoney).
                        tradeTime(new Date()).
                        tradeDigest("自己辛苦挣的血汗钱 " + savemoney).build();
        bankDao.addTrade(trade);

        return "钱已经存上了 -- 账户上又多了  " + savemoney + "  元";
    }

    /**
     * 给用户取钱
     */
    public String takemoney(String accountid, Double takemoney) {
        Double remainning = 0d;

        // 判断余额是否充足
        remainning = bankDao.selectRemaining(accountid);
        if (remainning < takemoney) {
            return "你都没有钱了 等着吃土吧" + String.valueOf(remainning);
        }
        // 取钱
        bankDao.transMoney(accountid, -takemoney);
        // 0 存款 1 取款 2 转账
        // 添加记录
        Trade trade =
                Trade.builder().accountId(accountid).
                        tradeType("1").
                        tradeMoney(takemoney).
                        tradeTime(new Date()).
                        tradeDigest("取钱养活女朋友 " + takemoney).build();
        bankDao.addTrade(trade);

        return "钱已经取出来了 账户上又少了  " + takemoney + "  元  余额是   " + (remainning - takemoney);
    }
}
